package com.paddyi.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.paddyi.commons.page.MyPage;
import com.paddyi.commons.utils.StringUtils;
import com.paddyi.mapper.UserMapper;
import com.paddyi.model.Role;
import com.paddyi.model.User;
import com.paddyi.model.vo.UserVo;
import com.paddyi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户 (User)
 *
 * @author paddy
 * @since 2019-04-26 04:34:10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserVo selectUserVoByLoginName(String loginName) {
        return userMapper.selectUserVoByLoginName(loginName);
    }

    @Override
    public UserVo selectUserVoById(Long id) {
        return userMapper.selectUserVoById(id);
    }

    @Override
    public Page<UserVo> selectUserVoPage(MyPage<User> page) {
        User user = page.getQuerycondition().get(0);
        Map map = new HashMap();
        map.put("pageLimit",(page.getCurrent()-1)*page.getSize()+","+page.getSize());
        if(StringUtils.isNotBlank(user.getName())){
            map.put("name",user.getName());
        }
        if(user.getOrganizationId()!=0){
            map.put("organization_id",user.getOrganizationId());
        }
        List<UserVo> userVos = userMapper.selectUserVoPage(map);
        for(UserVo u:userVos){
            if(u.getRoleList().size() > 0){
                StringBuffer sb = new StringBuffer();
                for(Role r:u.getRoleList()){
                    sb.append(r.getName()+",");
                }
                u.setRoleNames(sb.toString().substring(0,sb.length()-1));
            }
        }
        Page rePage = new Page();
        rePage.setRecords(userVos);
        rePage.setTotal(page.getTotal());
        return rePage;
    }

}
